package com.jdjw.modules.thirdpost.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.busassetarchives.entity.BusAssetArchives;
import com.jdjw.modules.busassetarchives.mapper.BusAssetArchivesMapper;
import com.jdjw.modules.thirdpost.param.BusAssetArchivesParam;
import com.jdjw.modules.thirdpost.param.ThirdPostInParm;
import com.jdjw.utils.CommonUtil;
import com.jdjw.utils.ConstantUtil;
import com.jdjw.utils.DateUtil;
import com.jdjw.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * SZ_ZCMX(8,"三资-资源型资产明细表","dwd_sz_zyxzcmx")
 */
@Service
@Slf4j
public class SzZyxzcmxbJiandeServiceImpl extends ServiceImpl<BusAssetArchivesMapper, BusAssetArchives>
        implements IService<BusAssetArchives> {

    @Autowired
    static BusAssetArchivesMapper busAssetArchivesMapper;

    @Autowired
    static ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl;

    @Resource
    public void setThirdPostExceptionServiceImpl(ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl) {
        SzZyxzcmxbJiandeServiceImpl.thirdPostExceptionServiceImpl = thirdPostExceptionServiceImpl;
    }

    @Resource
    public void setBusAssetArchivesMapper(BusAssetArchivesMapper busAssetArchivesMapper) {
        SzZyxzcmxbJiandeServiceImpl.busAssetArchivesMapper = busAssetArchivesMapper;
    }

    public static void doPostSzZyxzcmxbJiande(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 8;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 8;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 8;
        }
        try {
            //查询
            List<BusAssetArchives> list = busAssetArchivesMapper.selectListCwh(thirdPostInParm);
            if (list == null || list.size() < 1) {
                log.info("三资-资源型资产明细表数据库查询数据为0条");
                return;
            }
            if (list.size() > 3000) {
                Integer count = list.size() / 3000 + 1;
                Collection<Collection<BusAssetArchives>> fgList = CommonUtil.subCollection(list, count);
                for (Collection<BusAssetArchives> obj : fgList) {
                    sendData((List<BusAssetArchives>) obj, url);
                }
            } else {
                sendData(list, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void sendData(List<BusAssetArchives> obj, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (BusAssetArchives a : obj) {
            BusAssetArchivesParam param = new BusAssetArchivesParam();
            param.setBiz_id(a.getId());
            param.setId(a.getId());
            param.setCwh(a.getCwh());
            param.setCwhxzqh(CommonUtil.addZeroForNum(a.getOrgCode(), 16));
            param.setCmxz(a.getAreaCode());
            param.setZczybm(a.getZczybm());
            param.setZczymc(a.getName());//资产资源名称-资产名称
//            param.setZclb(Integer.parseInt(a.getZclb()));//数据库数据为空
//            param.setQdfs(Integer.parseInt(a.getQdfs()));//数据库数据为空
//            param.setQdrq(time(a.getQdrq()));//数据库数据为空
            param.setZczt(a.getStatus());
            param.setSymj(Double.parseDouble(a.getUseArea()));//数据太乱 已经加入正则
//            param.setGj(a.getGj());//数据库数据为空
            param.setApplicantid(a.getAddressDetail());//有空数据
            param.setApplicantphone(a.getContactPhone());
            param.setContactorid(a.getContactPersonNo());
            param.setPhone(a.getContactPhone());
            param.setCqzh(a.getPropertyCard());
//            param.setCqzlx(1);//数据库数据为空
            param.setDel_flag(0);
//            param.setCreateTime(a.getCreateTime());//数据库数据为空 类型转换：date--->string

            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("三资-资源型资产明细表推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostSzZyxzcmxbJiande", "三资-资源型资产明细表数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("三资-资源型资产明细表推送失败", result);
            }
        } else {
            log.info("三资-资源型资产明细表推送失败", "第三方返回空");
        }
    }

    /**
     * 时间转化(String--->date)
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date time(String date) throws ParseException {
        //string-date
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date jieguo = simpleDateFormat.parse(date);
        return jieguo;
    }

//    public static String uuid(){
//        UUID uuid = UUID.randomUUID();
//        String str = uuid.toString();
//        // 去掉"-"符号
//        String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
//        String yongyinleibie = "1";
//
//        return temp;
//    }

}
